package com.jzg.jcptadmin.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.jzg.jcptadmin.R;
import com.jzg.jcptadmin.Utils.MyToast;
import com.jzg.jcptadmin.app.AppManager;
import com.jzg.jcptadmin.base.BaseActivity;
import com.jzg.jcptadmin.constant.Constant;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.SuccessData;
import com.jzg.jcptadmin.presenter.ChangePdPresenter;
import com.jzg.jcptadmin.viewinterface.ChangePdInterface;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 密码修改
 * ChangePdActivity
 *
 * @author sunbl
 *         created at 2015/12/29 11:19
 */
public class ChangePdActivity extends BaseActivity implements View.OnClickListener, ChangePdInterface {

    @Bind(R.id.old_pass_text)
    TextView oldPassText;
    @Bind(R.id.old_pass_edit)
    EditText oldPassEdit;
    @Bind(R.id.new_pass_text)
    TextView newPassText;
    @Bind(R.id.new_pass_edit)
    EditText newPassEdit;
    @Bind(R.id.verify_pass_text)
    TextView verifyPassText;
    @Bind(R.id.verify_pass_edit)
    EditText verifyPassEdit;
    @Bind(R.id.title_content)
    TextView titleContent;
    @Bind(R.id.tv_right)
    TextView tvRight;
    private ChangePdPresenter changePdPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feed_back);
        ButterKnife.bind(this);
        titleContent.setText("修改密码");
        tvRight.setText("确认");
        changePdPresenter = new ChangePdPresenter(DataManager.getInstance());
        changePdPresenter.attachView(this);
    }

    @OnClick({R.id.tv_right})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_right:
                //提交修改的密码
                submitPassword();
                break;
            default:

                break;
        }


    }

    private void submitPassword() {
        String oldPass = oldPassEdit.getText().toString().trim();
        String newPass = newPassEdit.getText().toString().trim();
        String verifypass = verifyPassEdit.getText().toString().trim();

        if (oldPass.equals("")) {
            MyToast.showShort("原密码不能为空");
            return;
        }
        if (newPass.equals("")) {
            MyToast.showShort("新密码不能为空");
            return;
        }
        if (!newPass.equals(verifypass)) {
            MyToast.showShort("两次密码不一致");
            return;
        }
        if (oldPass.length() < 6 || newPass.length() < 6 || verifypass.length() < 6) {
            MyToast.showShort("密码长度不能小于6位");
            return;
        }
        showDialog();
        changePdPresenter.changePd(appContext.getUser().getUserId() + "", oldPass, newPass);


    }


    @Override
    public void showResult(SuccessData successData) {
        dismissDialog();
        if (successData.getStatus() == 100) {
            MyToast.showShort("修改成功,请重新登陆");
            Intent intent = new Intent(ChangePdActivity.this, MainActivity.class);
            intent.putExtra("edit", false);
            startActivity(intent);
            AppManager.getAppManager().finishAllActivity();
        } else if (successData.getStatus() == 101) {
            MyToast.showShort("旧密码错误");
        } else if (successData.getStatus() == 105) {
            MyToast.showShort("新旧密码为空或者用户为空");
        } else {
            MyToast.showShort( "修改失败");
        }

    }

    @Override
    public void showError(String error) {
        dismissDialog();
        MyToast.showShort(Constant.errr_net);

    }
}
